package javacommon.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javacommon.util.ConvertRegisterHelper;
import javacommon.util.PageRequestFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.cglib.core.ReflectUtils;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.util.Assert;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseStruts2Action extends ActionSupport implements RequestAware
{
   protected Map requestMap = null;
   // protected Map paramMap = null;

   static
   {
      // 注册converters
      ConvertRegisterHelper.registerConverters();
   }

   public void copyProperties(Object target, Object source)
   {
      BeanUtils.copyProperties(target, source);
   }

   public <T> T copyProperties(Class<T> destClass, Object orig)
   {
      return BeanUtils.copyProperties(destClass, orig);
   }

   public void setRequest(Map request)
   {
      this.requestMap = request;
   }

   public void savePage(Page page, PageRequest pageRequest)
   {
      savePage("", page, pageRequest);
   }

   /**
    * 用于一个页面有多个extremeTable是使用
    * 
    * @param tableId
    *           等于extremeTable的tableId属性
    */
   public void savePage(String tableId, Page page, PageRequest pageRequest)
   {
      Assert.notNull(tableId, "tableId must be not null");
      Assert.notNull(page, "page must be not null");

      getRequest().setAttribute(tableId + "page", page);
      getRequest().setAttribute(tableId + "totalRows", new Integer(page.getTotalCount()));
      getRequest().setAttribute(tableId + "pageRequest", pageRequest);
   }

   public PageRequest newPageRequest(String defaultSortColumns)
   {
      return PageRequestFactory.newPageRequest(ServletActionContext.getRequest(), defaultSortColumns);
   }

   public boolean isNullOrEmptyString(Object o)
   {
      return ObjectUtils.isNullOrEmptyString(o);
   }

   public HttpServletRequest getRequest()
   {
      HttpServletRequest request = ServletActionContext.getRequest();

      // 可注释掉
      // 处理所有参数
      java.util.Enumeration ep = request.getParameterNames();
 
      while (ep.hasMoreElements())
      {
         String paramName = (String) ep.nextElement();
         String paramValue = (String) request.getParameter(paramName);
         
         //参数filedata的内容不显示
         if(paramName != null && paramName.compareToIgnoreCase("filedata") != 0){
            System.out.println(paramName + " = " + paramValue);
         }
      }

      // 获取请求头信息
      java.util.Enumeration eh = request.getHeaderNames();
      while (eh.hasMoreElements())
      {
         String name = (String) eh.nextElement();
         String value = request.getHeader(name);
         //System.out.println(name + " = " + value);
      }

      return request;
   }

   public Map getParamMap()
   {
      Map paramMap = new HashMap();
      HttpServletRequest request = ServletActionContext.getRequest();
      // 处理所有参数
      java.util.Enumeration ep = request.getParameterNames();
      while (ep.hasMoreElements())
      {
         String paramName = (String) ep.nextElement();
         String paramValue = (String) request.getParameter(paramName);
         System.out.println("this is debug:" + paramName + " : " + paramValue);
         paramMap.put(paramName, paramValue);
      }
	  	String path = getRequest().getContextPath();
	  	String basePath = getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+path+"/";
	  	String webUrl = getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+"/web";
	  	String easyuiUrl = getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+path+"/resources/js/jquery-easyui-1.3.2";
	  	String imgUrl = getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+"/";
	  	String upUrl = getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort()+path+"/";
	  	/**应用部署绝对路径*/
	  	String sysWebPath =  getRequest().getSession().getServletContext().getRealPath("");
	  	
	  	paramMap.put("msUrl",basePath);
        return paramMap;
   }

   public HttpServletResponse getResponse()
   {
      return ServletActionContext.getResponse();
   }
   
     
   
}
